#!/usr/bin/env node
const inquirer = require('inquirer')
const path = require('path')
const fs = require('fs')
const ejs = require('ejs')

const templateDir = path.join(__dirname, 'template')
const destDir = process.cwd()

inquirer.prompt([
  {
    type: 'input',
    name: 'name',
    message: 'Project name?'
  },
  {
    type: 'list',
    name: 'theme',
    message: 'Which theme do you want?',
    choices: ['red', 'black', 'green', 'white']
  }
])
  .then(answers => {
    // 读取模板文件，根据用户输入，转化后输出到目标目录
    fs.readdir(templateDir, (err, files) => {
      if (err) throw err
      files.forEach(file => {
        ejs.renderFile(path.join(templateDir, file), answers, (err, result) => {
          if (err) throw err
          fs.writeFileSync(path.join(destDir, file), result)
        })
      })
    })
  })